package com.tech.connect.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.request.RequestOptions;
import com.ksy.common.image.ImageLoader;
import com.ksy.common.image.ImagePickActivity;
import com.ksy.common.image.ImageShowActivity;
import com.ksy.common.image.transformations.RoundedCornersTransformation;
import com.ksy.common.utils.AndroidUtil;
import com.ksy.common.utils.BaseHolder;
import com.tech.connect.R;
import com.tech.connect.activity.BaseActivity;

import java.util.ArrayList;
import java.util.List;

public class ImageAdder extends RecyclerView {
    private ArrayList<String> allImages = new ArrayList<>();
    private Adapter adapter;
    private BaseActivity baseActivity;
    private int total = 5;

    public List<String> getImages() {
        return allImages;
    }

    public ImageAdder(Context context) {
        this(context, null);
    }

    public ImageAdder(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ImageAdder(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
        setBackgroundResource(R.color.common_colorWhite);
        initAdapter(null);
    }

    public void bindContext(BaseActivity baseActivity, int total) {
        this.baseActivity = baseActivity;
        this.total = total;
    }

    public void onImagePickResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == 991) {
                if (data != null) {
                    ArrayList<String> paths = data.getStringArrayListExtra(ImagePickActivity.Data_selectImages);
                    initAdapter(paths);
                }
            }
        }
    }


    private void initAdapter(List<String> list) {
        if (list != null) {
            allImages.addAll(list);
        }
        if (adapter == null) {
            adapter = new Adapter(allImages);
            setAdapter(adapter);
        }
        adapter.notifyDataSetChanged();
    }

    private class Adapter extends RecyclerView.Adapter<VHolder> {
        private ArrayList<String> imageList;

        public Adapter(ArrayList<String> imageList) {
            this.imageList = imageList;
        }

        @Override
        public VHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            if (viewType == 1) {
                View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout_image_shower, parent, false);
                return new ImageHolder(inflate);
            } else {
                View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout_image_adder, parent, false);
                return new AdderHolder(inflate);
            }
        }

        @Override
        public void onBindViewHolder(VHolder holder, final int position) {
            if (holder instanceof AdderHolder) {
                ImageLoader.getInstance().load(getContext(), R.drawable.icon_image_adder, holder.iv, new RequestOptions().placeholder(R.drawable.icon_image_adder).transform(new RoundedCornersTransformation(AndroidUtil.dip2px(5), 0)));
                holder.iv.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int max = total - imageList.size();
                        ImagePickActivity.comeIn(baseActivity, true, max, 991);
                    }
                });
            } else if (holder instanceof ImageHolder) {
                String s = imageList.get(position);
                ImageLoader.getInstance().load(getContext(), s, holder.iv, new RequestOptions().transform(new RoundedCornersTransformation(AndroidUtil.dip2px(5), 0)));
                holder.ivDelete.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        imageList.remove(position);
                        notifyDataSetChanged();
                    }
                });
                holder.iv.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ImageShowActivity.showImages(baseActivity, imageList, position);
                    }
                });
            }

        }

        @Override
        public int getItemViewType(int position) {
            int imageNum = imageList.size();
            if (position == imageNum) {
                return -1;
            }
            return 1;
        }

        @Override
        public int getItemCount() {
            if (imageList.size() == total) {
                return imageList.size();
            }
            return imageList.size() + 1;
        }
    }


    private class VHolder extends BaseHolder {

        protected final ImageView iv;
        protected final View ivDelete;

        public VHolder(View itemView) {
            super(itemView);
            iv = itemView.findViewById(R.id.iv);
            ivDelete = itemView.findViewById(R.id.ivDelete);
        }
    }

    private class ImageHolder extends VHolder {

        public ImageHolder(View itemView) {
            super(itemView);
        }
    }

    private class AdderHolder extends VHolder {
        public AdderHolder(View itemView) {
            super(itemView);
        }
    }
}
